#!/bin/bash

DESKTOP_FILE="/usr/share/applications/figma-linux.desktop"
LINK="/usr/bin/figma-linux"

case $1 in
  -h|--help)
  echo "
figma-linux

Unofficial desktop application for linux. This application based on the Electron.js.

APPIMAGE OPTIONS:
    figma-linux.AppImage [option]

    OPTIONS:
        -i|--install      installs valid desktop file: /usr/share/applications/figma-linux.desktop, icons
                          and creates /usr/bin/figma-linux link to the AppImage file.

        -r|--remove       removes the desktop file, icons and link to the AppImage file.

FIGMA OPTIONS:
    figma-linux [option] [APP_URL_TO_PROJECT]

    OPTIONS:
        -v                show version of application.

COMMON OPTIONS:
        -h|--help         this reference.
    "
    exit
  ;;
  -i|--install)
    if [ "$EUID" -ne 0 ]; then
      echo "For install figma-linux AppImage run the file from root user"
      exit
    fi

    echo "install figma-linux.desktop as $DESKTOP_FILE"

    cp -rf "$APPDIR/figma-linux.desktop" "$DESKTOP_FILE"

    echo "create link $LINK"

    ln -s "$APPIMAGE" "$LINK"

    echo "install icons"

    for size in 24 36 48 64 72 96 128 192 256 384 512; do
      mkdir -p "/usr/share/icons/hicolor/${size}x${size}/apps"
      mkdir -p "/usr/share/icons/hicolor/scalable/apps"
      cp -rf "$APPDIR/icons/${size}x${size}.png" "/usr/share/icons/hicolor/${size}x${size}/apps/figma-linux.png"
    done

    cp -rf "$APPDIR/icons/scalable.svg" "/usr/share/icons/hicolor/scalable/apps/figma-linux.svg"

    echo "install completed"

    exit
  ;;
  -r|--remove)
    if [ "$EUID" -ne 0 ]; then
      echo "For remove figma-linux AppImage and related files run the file from root user"
      exit
    fi

    echo "remove $DESKTOP_FILE"

    rm -rf "$DESKTOP_FILE"

    echo "remove $LINK"

    rm "$LINK"

    echo "remove icons"

    for size in 24 36 48 64 72 96 128 192 256 384 512; do
      rm -rf "/usr/share/icons/hicolor/${size}x${size}/apps/figma-linux.png"
    done

    rm -rf "/usr/share/icons/hicolor/scalable/apps/figma-linux.svg"

    echo "remove $APPIMAGE"

    rm -rf "$APPIMAGE"

    echo "remove completed"

    exit
  ;;
esac

"$APPDIR/figma-linux" $@
